package com.medical.lsq.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.medical.lsq.dao.SysInstitutionMapper;
import com.medical.lsq.dao.SysNhorganMapper;
import com.medical.lsq.entity.SysInstitution;
import com.medical.lsq.entity.SysNhorgan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;

@Service
@Transactional
public class InstitutionService {

    @Autowired
    private SysInstitutionMapper institutionMapper;

    /**
     * 添加医疗机构信息
     * @param sysInstitution
     */
    public void add(SysInstitution sysInstitution) {
        sysInstitution.setCreateTime(new Timestamp(System.currentTimeMillis()));
        sysInstitution.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        institutionMapper.insertSelective(sysInstitution);
    }

    /**
     * 根据主键ID删除一条信息
     * @param id
     */
    public void delete(Integer id) {institutionMapper.deleteByPrimaryKey(id);}

    /**
     * 根据对象信息进行更新
     * @param sysInstitution
     */
    public void edit(SysInstitution sysInstitution) {
        sysInstitution.getUpdateTime(new Timestamp(System.currentTimeMillis()));
        institutionMapper.updateByPrimaryKeySelective(sysInstitution);
    }
    /**
     * 查询所有医疗机构信息，可以进行模糊查询，需要进行分页
     * @return 医疗机构信息列表
     */
    public PageInfo<SysInstitution> getInstitutionList(Integer currentPage, Integer limit, String instituName) {
        //利用PageHelper分页查询 注意：这个一定要放查询语句的前一行,否则无法进行分页,因为它对紧随其后第一个sql语句有效
        PageHelper.startPage(currentPage, limit);
        List<SysInstitution> list = institutionMapper.getInstitutionList(instituName);
        PageInfo<SysInstitution> pageInfo = new PageInfo<SysInstitution>(list);
        return pageInfo;
    }
    /**
     * 通过主键查找一条农合机构信息
     * @param id
     * @return
     */
    public SysInstitution getInstiyution(Integer id) {
        return institutionMapper.selectByPrimaryKey(id);
    }
}
